/**
 * Berechnungs Chart und Prints für Liveansicht
 * @author admin
 */
package temprom.GUI;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.*;
 
/**
 *
 * @author admin
 */
public class FrmChart extends JPanel {

    int[] data = {
        //0,10,20,30,0,5
    };

    /**
     *
     */
    public ArrayList<String> params;
    final int PAD = 20;
    final int SKALA = 4;
    private int param1Value = 2650;
    private double param1Temperture = 20;
    private int param2 = 2300;
    private int param3 = 2700;
    Graphics2D g2;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        
        
        int w = getWidth();
        int h = getHeight();
        // Y-Achse zeichnen
        //g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
        // Draw abcissa. (X)
        g2.draw(new Line2D.Double(PAD, h/2, w-PAD, h/2));
        // Draw labels.
        Font font = g2.getFont();
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = font.getLineMetrics("20", frc);
        float sh = lm.getAscent() + lm.getDescent();

        // Labels und Lines für Y-Achse
        String[] yLabels={"-5","0","5","10","15","20","25","30","35","40","45"};
        
        float sy = h-PAD;//PAD  + lm.getAscent();
        g2.setPaint(Color.gray);
        for (int i = 0; i < yLabels.length; i++) {
            if (i == 5)
            {
                g2.setStroke(new BasicStroke(3));
            }
            g2.draw(new Line2D.Double(PAD, sy,w-PAD, sy));
            g2.setStroke(new BasicStroke(1));
            g2.drawString(yLabels[i], 3, sy);
            sy -= 20;
        }

        // Label für X-Achse.
        String s = "20 °Celsius";
        sy = h/2 + (PAD - sh)/2 + lm.getAscent();
        float sw = (float)font.getStringBounds(s, frc).getWidth();
        float sx = (w - sw)/2;
        
        g2.drawString(s, sx, sy);

        drawLines(h, w);
    }
 
    private int getMax() {
        int max = -Integer.MAX_VALUE;
        for(int i = -0; i < data.length; i++) {
            if(data[i] > max)
                max = data[i];
        }
        return max;
    }
    
    /**
     *
     * @param h
     * @param w
     */
    public void drawLines(double h,double w)
    {
        /**
         * Draw lines.
         */
        double breite = getWidth();
        double hoehe = getHeight();
        //double xInc = (double)(w - 2*PAD)/(data.length-1);
        //double scale = (double)(h - 2*PAD)/getMax();
        
        double x = data.length * 8;
        
        g2.setPaint(Color.green.darker());
        for(int i = 0; i < data.length-1; i++) {
            //double x1 = breite - (PAD + i*xInc);
            //double y1 = h - PAD - scale*data[i];
            double y1 = (hoehe-40)-CalculateTemperture(data[i])*SKALA;
            //double x2 = breite - (PAD + (i+1)*xInc);
            double y2 = (hoehe-40)-CalculateTemperture(data[i+1])*SKALA;
            //double y2 = h - PAD - scale*data[i+1];
            g2.draw(new Line2D.Double(breite-x, y1, breite-x+8, y2));
            x -= 8;
        }     
        /**
         * Mark data points.
         */
       
        g2.setPaint(Color.red);
        x = data.length * 8;
        for(int i = 0; i < data.length; i++) {
            //breite - (PAD + i*xInc);
            //double y = h - PAD - scale*data[i];
            double y = (hoehe-40)-CalculateTemperture(data[i])*SKALA;
            //g2.fill(new Ellipse2D.Double(breite-x-2, hoehe-y-2, 4, 4));
            g2.fill(new Ellipse2D.Double(breite-x-2,y-2,4,4));
            x -= 8;
        }
        repaint();
    }
    /**
     * Berechnet die Temperatur aus den Daten des USB-Geräts und den Parameter aus der Voreingestellten admin Konsole
     * 
     * @param value = Temperaturwert von SUB-Gerät
     * 
     * Return: Doublewert als Temperatur
     * @return  
     * Chek
     */
    public double CalculateTemperture(int value)
    {
        double result = 0.0;
        if (value > 0)
        {
        double messungX0 = Double.parseDouble(params.get(1));
        double messungY0 = Double.parseDouble(params.get(0));
        
        double messungX1 = Double.parseDouble(params.get(3));
        double messungY1 = Double.parseDouble(params.get(2));
        
        double messungX2 = Double.parseDouble(params.get(5));
        double messungY2 = Double.parseDouble(params.get(4));
        
        double messungX3 = Double.parseDouble(params.get(7));
        double messungY3 = Double.parseDouble(params.get(6));
        
        double a1 = (messungX0 - messungX1) / (messungY0 - messungY1);
        //System.out.println(a1);
        double a2 = (messungX1 - messungX2) / (messungY1 - messungY2);
        //System.out.println(a2);
        double a3 = (messungX2 - messungX3) / (messungY2 - messungY3);
        //System.out.println(a3);     
        double b1 = (a1-a2) / (messungY0 - messungY2);
        //System.out.println(b1);
        double b2 = (a2-a3) / (messungY1 - messungY3);
        //System.out.println(b2);
        double c1 = (b1-b2) / (messungY0 - messungY3);
        //System.out.println(c1);
         result = messungX0
         + (a1*(value-messungY0))
         + (b1*(value-messungY0)*(value-messungY1))
         + (c1*(value-messungY0)*(value-messungY1)*(value-messungY2));
   }
        //System.out.println(result1);
        return result;
     }
    /**
     *
     * @param parameters
     */
    public void SetParameters(ArrayList<String> parameters)
    {
      params = parameters;
    }
}


